{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**我们需要找到我们的颜色 并且使用网络摄像头**\n",
    "\n",
    "**找到颜色的认为地方放置不同的点并且创建绘画示例**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "frameWidth = 640\n",
    "frameHeight = 480\n",
    "cap = cv2.VideoCapture(0)\n",
    "cap.set(3, frameWidth)\n",
    "cap.set(4, frameHeight)\n",
    "cap.set(10,150)\n",
    "\n",
    "# 检测的颜色\n",
    "myColors = [[5,107,0,19,255,255],\n",
    "            [133,56,0,159,156,255],\n",
    "            [57,76,0,100,255,255],\n",
    "            [90,48,0,118,255,255]]\n",
    "\n",
    "# 颜色的BGR值\n",
    "myColorValues = [[51,153,255],          # BGR\n",
    "                 [255,0,255],\n",
    "                 [0,255,0],\n",
    "                 [255,0,0]]\n",
    "\n",
    "myPoints =  []  ## [x , y , colorId ]\n",
    "\n",
    "# 图片的颜色\n",
    "def findColor(img,myColors,myColorValues):\n",
    "    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "    count = 0\n",
    "    newPoints=[]\n",
    "    for color in myColors:\n",
    "        lower = np.array(color[0:3])\n",
    "        upper = np.array(color[3:6])\n",
    "        mask = cv2.inRange(imgHSV,lower,upper)\n",
    "        x,y=getContours(mask)\n",
    "        cv2.circle(imgResult,(x,y),15,myColorValues[count],cv2.FILLED)\n",
    "        if x!=0 and y!=0:\n",
    "            newPoints.append([x,y,count])\n",
    "        count +=1\n",
    "        #cv2.imshow(str(color[0]),mask)\n",
    "    return newPoints\n",
    "\n",
    "# 图片的轮廓\n",
    "def getContours(img):\n",
    "    contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)\n",
    "    x,y,w,h = 0,0,0,0\n",
    "    for cnt in contours:\n",
    "        area = cv2.contourArea(cnt)\n",
    "        if area>500:\n",
    "            #cv2.drawContours(imgResult, cnt, -1, (255, 0, 0), 3)\n",
    "            peri = cv2.arcLength(cnt,True)\n",
    "            approx = cv2.approxPolyDP(cnt,0.02*peri,True)\n",
    "            x, y, w, h = cv2.boundingRect(approx)\n",
    "    return x+w//2,y\n",
    "\n",
    "def drawOnCanvas(myPoints,myColorValues):\n",
    "    for point in myPoints:\n",
    "        cv2.circle(imgResult, (point[0], point[1]), 10, myColorValues[point[2]], cv2.FILLED)\n",
    "\n",
    "\n",
    "while True:\n",
    "    success, img = cap.read()\n",
    "    imgResult = img.copy()\n",
    "    newPoints = findColor(img, myColors,myColorValues)\n",
    "    if len(newPoints)!=0:\n",
    "        for newP in newPoints:\n",
    "            myPoints.append(newP)\n",
    "    if len(myPoints)!=0:\n",
    "        drawOnCanvas(myPoints,myColorValues)\n",
    "\n",
    "\n",
    "    cv2.imshow(\"Result\", imgResult)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
